import cv2 as cv
from python_ai.common.xcommon import *
import numpy as np
import os
import sys
import datetime
import matplotlib.pyplot as plt

spr = 2
spc = 2
spn = 0
plt.figure(figsize=[6, 6])


def show_img(title, img, trans=None, **kwargs):
    global spn
    spn += 1
    if spn > spr * spc:
        return
    plt.subplot(spr, spc, spn)
    plt.axis('off')
    if trans is not None:
        img = trans(img)
    plt.imshow(img, **kwargs)


img = cv.imread('../../../../large_data/pic/opencv-logo-white.png', cv.IMREAD_COLOR)
img_shape = img.shape
IMG_H = img_shape[0]
IMG_W = img_shape[1]
show_img('ori', img, lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

# translation
M = np.float32([
    [1, 0, 40],
    [0, 1, 20]
])
img_move = cv.warpAffine(img, M, (IMG_W, IMG_H))
show_img('move', img_move, lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

# rotate
M = cv.getRotationMatrix2D((IMG_W // 2, IMG_H // 2), 45, 2)
img_rotate = cv.warpAffine(img, M, (IMG_W, IMG_H))
show_img('rotate', img_rotate, lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))
